package com.goods.controller.business;

import com.github.pagehelper.PageHelper;
import com.goods.business.service.ProductService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * 物质资料
 */
@Api(tags = "业务模块-物资资料相关接口")
@RestController
@RequestMapping("/business/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "物资资料",notes = "显示物资资料列表")
    @GetMapping("/findProductList")
    public ResponseBean<PageVO<ProductVO>> findProductList(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "6") Integer pageSize,
            @RequestParam(value = "name",defaultValue = "") String name,

            @RequestParam(value = "categoryId",defaultValue = "") String categoryId,
            @RequestParam(value = "supplier",defaultValue = "") String supplier,
            @RequestParam(value = "status",defaultValue = "0") Integer status,
            @RequestParam(value = "categorys",defaultValue = "") Long[] categorys,

            ProductVO productVO){

        PageVO<ProductVO> findProductList=productService.findProductList(
                pageNum,pageSize,name,categoryId,
                supplier,status,categorys,productVO);
        return ResponseBean.success(findProductList);
    }


    /*添加或编辑物质*/
    @ApiOperation(value = "物资添加",notes = "显示物资添加")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO){
        Boolean isAdded=productService.add(productVO);
        if (isAdded){
            return ResponseBean.success("添加成功");
        }
        return ResponseBean.error(BusinessCodeEnum.PRODUCT_IN_STOCK_EMPTY);
    }

    /*编辑物资前回显数据*/
    @ApiOperation(value = "编辑物资",notes = "编辑物资")
    @GetMapping("/edit/{id}")
    public ResponseBean<ProductVO> edit(@PathVariable Integer id){
        ProductVO productVO=productService.edit(id);
        return ResponseBean.success(productVO);

    }

    /*编辑物资更新数据*/
    @ApiOperation(value = "编辑物资进行更新",notes = "编辑物资更新数据保存")
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody ProductVO productVO){
        Boolean isAdded=productService.update(productVO);
        if (isAdded){
            return ResponseBean.success("添加成功");
        }
        return ResponseBean.error(BusinessCodeEnum.PRODUCT_IN_STOCK_EMPTY);
    }

    /*将物资放入回收箱*/
    @ApiOperation(value = "将物资放入回收箱",notes = "将物资放入回收箱")
    @PutMapping("/remove/{id}")
    public ResponseBean remove(
            @PathVariable Integer id){
        Boolean isRemove=productService.remove(id);
        if (isRemove){
            return ResponseBean.success(BusinessCodeEnum.PRODUCT_IS_REMOVE);
        }
        return ResponseBean.error(BusinessCodeEnum.PRODUCT_NOT_FOUND);
    }

    /*将物资从回收箱里面恢复*/
    @ApiOperation(value = "将物资从回收箱里面恢复",notes = "将物资从回收箱里面恢复")
    @PutMapping("/back/{id}")
    public ResponseBean back(
            @PathVariable Integer id){
        Boolean isRemove=productService.back(id);
        if (isRemove){
            return ResponseBean.success("已恢复成功");
        }
        return ResponseBean.error("恢复失败");
    }

    /*将物资从回收站中删除*/
    @ApiOperation(value = "将物资从回收站中删除",notes = "将物资从回收站中删除")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){

        Boolean isDeleted=productService.delete(id);
        if (isDeleted){
            return ResponseBean.success("物资删除成功");
        }
        return ResponseBean.error("物资删除失败");

    }


    /*将物资从审核中通过*/
    @ApiOperation(value = "将物资从审核中通过",notes = "将物资从审核中通过")
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){

        Boolean isDeleted=productService.publish(id);
        if (isDeleted){
            return ResponseBean.success("物资删除成功");
        }
        return ResponseBean.error("物资删除失败");

    }





}
